package net.wwang.open.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author WangWei
 * @date 2020/8/11
 */
@Slf4j
public class PropertiesUtil {
    private static final String DEFAULT_PROPERTIES = "application.properties";

    /**
     * 获取properties属性值
     *
     * @param str
     * @return
     */
    public static String getProp(String str) {
        try {
            Resource resource = new ClassPathResource(DEFAULT_PROPERTIES);
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            return props.getProperty(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Properties loadProperties(String propertiesFileName) {
        try {
            Properties properties = new Properties();
            InputStream fis = Utils.class.getClassLoader().getResourceAsStream(propertiesFileName);
            properties.load(fis);
            return properties;
        } catch (IOException e) {
            log.error("Configuration file " + propertiesFileName + " is not found");
            return new Properties();
        }
    }
}
